//
//  PersonChangeNameViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/8/13.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PersonChangeNameViewController.h"

@interface PersonChangeNameViewController ()

@property (strong, nonatomic) UITextField * nameTextFiled;
@property (strong, nonatomic) UIButton * btn;
@property (strong, nonatomic) UIView * linView;

@end

@implementation PersonChangeNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改昵称";
    [self.view addSubview:self.nameTextFiled];
    [self.view addSubview:self.linView];
    [self.view addSubview:self.btn];
}

-(UITextField *)nameTextFiled
{
    if (_nameTextFiled == nil) {
        _nameTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(70, kStatusAndNavBarH+ 60, WIDTH-140, 30)];
        _nameTextFiled.placeholder = @"请输入昵称";
        _nameTextFiled.textAlignment = NSTextAlignmentCenter;
        [_nameTextFiled addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
        _nameTextFiled.placeholder = self.name;
    }
    return _nameTextFiled;
}

-(void)textChange
{
    NSString * str = self.nameTextFiled.text;
    if (str.length>12) {
        self.nameTextFiled.text = [str substringToIndex:12];
    }
}

-(UIView *)linView
{
    if (_linView == nil) {
        _linView =[[UIView alloc]initWithFrame:CGRectMake(70, kStatusAndNavBarH+91, WIDTH-140, 1)];
        _linView.backgroundColor=SFMColor(204, 204, 204);
    }
    return _linView;
}

-(UIButton *)btn
{
    if (_btn == nil) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(40, kStatusAndNavBarH + 142, WIDTH-80, 40)];
        _btn.titleLabel.font = font(17);
        [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btn setTitle:@"提交" forState:0];
        _btn.layer.cornerRadius = 4;
        [_btn setBackgroundColor:[UIColor redColor]];
    }
    return _btn;
}

-(void)btnClick:(UIButton *)btn
{
    if (self.nameTextFiled.text.length == 0) {
        [MBProgressHUD showMessageWithDelay:@"请输入昵称" delay:2];
        return;
    }
    NSSLog(@"提交=== ");

    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params setValue:SFMuserID forKey:@"id"];
    [params setValue:self.nameTextFiled.text forKey:@"bxrName"];
    WeakSelf;
    btn.userInteractionEnabled = NO;
    
    dispatch_queue_t queue = dispatch_queue_create("netWork", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSString * url = [BASE_URL stringByAppendingString:@"/rest/appLoginController/updateAppUser"];
        NSString * path = [url stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
        
        AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
        [manager.requestSerializer setValue:@"*/*" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:SFMToken forHTTPHeaderField:@"X-AUTH-TOKEN"];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/html", nil];
        [manager POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
           
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary * dic = (NSDictionary *)responseObject;
            NSSLog(@"dic=== %@",dic);
            NSString * codeStr = [NSString stringWithFormat:@"%@",dic[@"respCode"]];
            if ([codeStr isEqualToString:@"0"]) {
                [SVProgressHUD ShowSuccessMessage:@"修改成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    weakSelf.personChangeNameEventBlock(weakSelf.nameTextFiled.text);
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"personDidChangeName" object:nil userInfo:@{@"name":weakSelf.nameTextFiled.text}];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
            }else
            {
                [SVProgressHUD ShowErrorMessage:dic[@"respMsg"]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                btn.userInteractionEnabled = YES;
            });
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSSLog(@"error=== %@",error);
            dispatch_async(dispatch_get_main_queue(), ^{
                btn.userInteractionEnabled = YES;
            });
        }];
    });
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
